package com.ck567.netty.config

import io.netty.channel.socket.nio.NioServerSocketChannel

import io.netty.bootstrap.ServerBootstrap

import io.netty.channel.nio.NioEventLoopGroup

import io.netty.channel.ChannelFuture

import io.netty.channel.EventLoopGroup
import org.springframework.stereotype.Component


/**
 * 服务端基本配置，通过一个静态单例类，保证启动时候只被加载一次
 */
@Component
class WssServer {
    /**
     * 单例静态内部类
     * @author asus
     */
    object SingletionWSServer {
        val instance = WssServer()
    }

    private val mainGroup: EventLoopGroup
    private val subGroup: EventLoopGroup
    private val server: ServerBootstrap
    private var future: ChannelFuture? = null
    fun start() {
        future = server.bind(8087)
        System.err.println("netty 服务端启动完毕 .....")
    }

    companion object {
        val instance: WssServer
            get() = SingletionWSServer.instance
    }

    init {
        mainGroup = NioEventLoopGroup()
        subGroup = NioEventLoopGroup()
        server = ServerBootstrap()
        server.group(mainGroup, subGroup)
            .channel(NioServerSocketChannel::class.java)
            .childHandler(WssServerInitialzer()) //添加自定义初始化处理器
    }
}